<template>
  <PageView>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="渠道名称">
                <a-tree-select
                  v-model="queryParam.channel"
                  style="width: 100%"
                  :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                  :treeData="projectTree"
                  placeholder="请选择渠道..."
                  treeDefaultExpandAll
                >
                  <span slot="title" slot-scope="{ id }">{{ id }}
                  </span>
                </a-tree-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
      >
        <template slot="operator">
          <span class="table-title">微信报警列表</span>
        </template>

        <span slot="content" slot-scope="text">
          <ellipsis :length="140" tooltip>{{ text }}</ellipsis>
        </span>

        <span slot="action" slot-scope="text, record">
          <a>详情</a>
            <a-divider type="vertical" v-if="hasPerm('alarm:edit') & hasPerm('alarm:delete')"/>
            <a-popconfirm v-if="hasPerm('alarm:delete')" placement="topRight" title="确认删除？"
                          @confirm="() => alarmDelete(record.id)">
              <a style="color: red">删除</a>
            </a-popconfirm>
          </span>

      </s-table>
    </a-card>
  </PageView>
</template>
<script>
  import { PageView } from '@/layouts'
  import { STable, Ellipsis, XCard } from '@/components'
  import * as api from '@/api/system/alarm'
  import { getProjectTree } from '@/api/main/project'

  export default {
    components: {
      PageView,
      Ellipsis,
      XCard,
      STable
    },
    data() {
      return {
        advanced: false,
        // 查询参数
        queryParam: {},
        projectTree: [],
        // 表头
        columns: [
          {
            title: '项目',
            dataIndex: 'project'
          },
          {
            title: '渠道',
            dataIndex: 'channel_name'
          },
          {
            title: '报警标题',
            dataIndex: 'title'
          },
          {
            title: '报警内容',
            dataIndex: 'content',
            scopedSlots: { customRender: 'content' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return api.getPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        opTypeDict: []
      }
    },
    created() {
      if (this.hasPerm('project:edit') || this.hasPerm('project:delete')) {
        this.columns.push({
          title: '操作',
          width: '150',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
      getProjectTree().then((res) => {
        this.projectTree = res.data.rows
      })
    },
    methods: {
      handleOk() {
        this.$refs.table.refresh()
      },
      alarmDelete(id) {
        api.deleteObj(id).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh(true)
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-title-right {
    float: right;
    margin-right: 30px;
  }
</style>
